/*     */ package com.wgcloud.controller;
/*     */ 
/*     */ import cn.hutool.json.JSONObject;
/*     */ import cn.hutool.json.JSONUtil;
/*     */ import com.github.pagehelper.PageInfo;
/*     */ import com.wgcloud.config.CommonConfig;
/*     */ import com.wgcloud.entity.ShellInfo;
/*     */ import com.wgcloud.entity.ShellState;
/*     */ import com.wgcloud.entity.SystemInfo;
/*     */ import com.wgcloud.service.LogInfoService;
/*     */ import com.wgcloud.service.ShellInfoService;
/*     */ import com.wgcloud.service.ShellStateService;
/*     */ import com.wgcloud.service.SystemInfoService;
/*     */ import com.wgcloud.util.FormatUtil;
/*     */ import com.wgcloud.util.HostUtil;
/*     */ import com.wgcloud.util.PageUtil;
/*     */ import com.wgcloud.util.ResDataUtils;
/*     */ import com.wgcloud.util.ThreadPoolUtil;
/*     */ import com.wgcloud.util.TokenUtils;
/*     */ import com.wgcloud.util.license.LicenseUtil;
/*     */ import com.wgcloud.util.msg.WarnMailUtil;
/*     */ import com.wgcloud.util.staticvar.StaticKeys;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.annotation.Resource;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import org.apache.commons.lang3.StringUtils;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.stereotype.Controller;
/*     */ import org.springframework.ui.Model;
/*     */ import org.springframework.web.bind.annotation.RequestBody;
/*     */ import org.springframework.web.bind.annotation.RequestMapping;
/*     */ import org.springframework.web.bind.annotation.ResponseBody;
/*     */ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ @Controller
/*     */ @RequestMapping({"/shellInfo"})
/*     */ public class ShellInfoController
/*     */ {
/*  49 */   private static final Logger logger = LoggerFactory.getLogger(com.wgcloud.controller.ShellInfoController.class);
/*     */ 
/*     */   
/*     */   @Resource
/*     */   private ShellInfoService shellInfoService;
/*     */ 
/*     */   
/*     */   @Resource
/*     */   private ShellStateService shellStateService;
/*     */ 
/*     */   
/*     */   @Resource
/*     */   private LogInfoService logInfoService;
/*     */ 
/*     */   
/*     */   @Resource
/*     */   private SystemInfoService systemInfoService;
/*     */ 
/*     */   
/*     */   @Autowired
/*     */   private TokenUtils tokenUtils;
/*     */ 
/*     */   
/*     */   @Autowired
/*     */   CommonConfig commonConfig;
/*     */ 
/*     */   
/*     */   @ResponseBody
/*     */   @RequestMapping({"agentList"})
/*     */   public String agentList(@RequestBody String paramBean) {
/*  79 */     JSONObject agentJsonObject = (JSONObject)JSONUtil.parse(paramBean);
/*  80 */     if (!this.tokenUtils.checkAgentToken(agentJsonObject)) {
/*  81 */       logger.error("token is error");
/*  82 */       return ResDataUtils.resetErrorJson("token is error");
/*     */     } 
/*  84 */     if (!"true".equals(this.commonConfig.getShellToRun())) {
/*  85 */       return ResDataUtils.resetErrorJson("shellToRun is no open");
/*     */     }
/*  87 */     Map<String, Object> params = new HashMap<>();
/*  88 */     if (null == agentJsonObject.get("hostname") || StringUtils.isEmpty(agentJsonObject.get("hostname").toString())) {
/*  89 */       return "";
/*     */     }
/*  91 */     params.put("hostname", agentJsonObject.get("hostname").toString());
/*     */     try {
/*  93 */       params.put("state", "1");
/*  94 */       List<ShellState> shellStateList = this.shellStateService.selectAllByParams(params);
/*  95 */       for (ShellState state : shellStateList) {
/*  96 */         state.setShellToRunLinuxBlock(this.commonConfig.getShellToRunLinuxBlock());
/*  97 */         state.setShellToRunWinBlock(this.commonConfig.getShellToRunWinBlock());
/*     */       } 
/*  99 */       return ResDataUtils.resetSuccessJson(shellStateList);
/* 100 */     } catch (Exception e) {
/* 101 */       logger.error("agent获取下发指令信息错误", e);
/* 102 */       this.logInfoService.save("agent获取下发指令信息错误", e.toString(), "1");
/* 103 */       return ResDataUtils.resetErrorJson(e.toString());
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @ResponseBody
/*     */   @RequestMapping({"/shellCallback"})
/*     */   public String appCallback(@RequestBody String paramBean) {
/* 116 */     JSONObject agentJsonObject = (JSONObject)JSONUtil.parse(paramBean);
/* 117 */     logger.info("shellCallback-------------" + agentJsonObject.toString());
/* 118 */     if (!this.tokenUtils.checkAgentToken(agentJsonObject)) {
/* 119 */       logger.error("token is error");
/* 120 */       return ResDataUtils.resetErrorJson("token is error");
/*     */     } 
/*     */     try {
/* 123 */       String shellStateId = agentJsonObject.getStr("id");
/* 124 */       if (StringUtils.isEmpty(shellStateId)) {
/* 125 */         logger.error("shellStateId is null");
/* 126 */         return ResDataUtils.resetErrorJson("shellStateId is null");
/*     */       } 
/* 128 */       String shellState = agentJsonObject.getStr("state");
/* 129 */       if (!StringUtils.isEmpty(shellState) && shellState.length() > 500) {
/* 130 */         shellState = shellState.substring(0, 500);
/*     */       }
/* 132 */       ShellState shellStateCall = this.shellStateService.selectById(shellStateId);
/* 133 */       shellStateCall.setState(shellState);
/* 134 */       shellStateCall.setCreateTime(new Date());
/* 135 */       this.shellStateService.updateById(shellStateCall);
/* 136 */       return ResDataUtils.resetSuccessJson(null);
/* 137 */     } catch (Exception e) {
/* 138 */       e.printStackTrace();
/* 139 */       logger.error("agent执行指令完成回调错误", e);
/* 140 */       return ResDataUtils.resetErrorJson(e.toString());
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"list"})
/*     */   public String ShellInfoList(ShellInfo shellInfo, Model model, HttpServletRequest request) {
/* 153 */     Map<String, Object> params = new HashMap<>();
/*     */     
/*     */     try {
/* 156 */       LicenseUtil.maxLicense_10(model, request);
/*     */ 
/*     */       
/* 159 */       StringBuffer url = new StringBuffer();
/* 160 */       String shellName = null;
/* 161 */       if (!StringUtils.isEmpty(shellInfo.getShellName())) {
/* 162 */         shellName = shellInfo.getShellName();
/* 163 */         params.put("shellName", shellName.trim());
/* 164 */         url.append("&shellName=").append(shellName);
/*     */       } 
/* 166 */       if (!StringUtils.isEmpty(shellInfo.getState())) {
/* 167 */         params.put("state", shellInfo.getState());
/* 168 */         url.append("&state=").append(shellInfo.getState());
/*     */       } 
/* 170 */       PageInfo<ShellInfo> pageInfo = this.shellInfoService.selectByParams(params, shellInfo.getPage().intValue(), shellInfo.getPageSize().intValue());
/*     */       
/* 172 */       Map<String, Object> paramsStateCount = new HashMap<>();
/* 173 */       for (ShellInfo shellInfo1 : pageInfo.getList()) {
/* 174 */         paramsStateCount.clear();
/* 175 */         paramsStateCount.put("shellId", shellInfo1.getId());
/* 176 */         Integer stateAllCount = Integer.valueOf(this.shellStateService.countByParams(paramsStateCount));
/* 177 */         paramsStateCount.put("state", "1");
/* 178 */         Integer state1Count = Integer.valueOf(this.shellStateService.countByParams(paramsStateCount));
/* 179 */         shellInfo1.setState1Count(state1Count);
/* 180 */         shellInfo1.setStateOtherCount(Integer.valueOf(stateAllCount.intValue() - state1Count.intValue()));
/* 181 */         if (state1Count.intValue() == 0) {
/* 182 */           shellInfo1.setState("3");
/*     */         }
/*     */       } 
/*     */       
/* 186 */       PageUtil.initPageNumber(pageInfo, model);
/* 187 */       model.addAttribute("pageUrl", "/shellInfo/list?1=1" + url.toString());
/* 188 */       model.addAttribute("page", pageInfo);
/* 189 */       model.addAttribute("shellInfo", shellInfo);
/* 190 */       model.addAttribute("shellToRun", this.commonConfig.getShellToRun());
/* 191 */     } catch (Exception e) {
/* 192 */       logger.error("查询下发指令信息错误", e);
/* 193 */       this.logInfoService.save("查询下发指令信息错误", e.toString(), "1");
/*     */     } 
/* 195 */     return "shellInfo/list";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"save"})
/*     */   public String saveShellInfo(ShellInfo shellInfo, Model model, HttpServletRequest request) {
/* 209 */     if (!"true".equals(this.commonConfig.getShellToRun())) {
/* 210 */       return ResDataUtils.resetErrorJson("shellToRun is no open");
/*     */     }
/*     */     
/*     */     try {
/* 214 */       String blockKey = FormatUtil.haveBlockDanger(shellInfo.getShell(), this.commonConfig.getShellToRunLinuxBlock(), this.commonConfig.getShellToRunWinBlock());
/* 215 */       if (!StringUtils.isEmpty(blockKey)) {
/* 216 */         model.addAttribute("shellInfo", shellInfo);
/* 217 */         String[] selectedHostnames = request.getParameterValues("hostnames");
/* 218 */         model.addAttribute("selectedHosts", selectedHostnames);
/* 219 */         List<SystemInfo> systemInfoList = this.systemInfoService.selectAllByParams(new HashMap<>());
/* 220 */         for (SystemInfo systemInfo : systemInfoList) {
/* 221 */           for (String selectedHost : selectedHostnames) {
/* 222 */             if (selectedHost.equals(systemInfo.getHostname())) {
/* 223 */               systemInfo.setSelected("selected");
/*     */             }
/*     */           } 
/*     */         } 
/* 227 */         model.addAttribute("systemInfoList", systemInfoList);
/* 228 */         model.addAttribute("msg", "下发指令含有敏感字符" + blockKey + "，请检查");
/*     */         
/* 230 */         getBlockStr(model);
/* 231 */         return "shellInfo/add";
/*     */       } 
/*     */ 
/*     */       
/* 235 */       if (StringUtils.isEmpty(shellInfo.getId())) {
/* 236 */         String[] hostnames = request.getParameterValues("hostnames");
/* 237 */         if (null == hostnames || hostnames.length < 1) {
/* 238 */           return "redirect:/shellInfo/list";
/*     */         }
/* 240 */         this.shellInfoService.save(shellInfo, hostnames);
/*     */         
/* 242 */         Runnable runnable = () -> {
/*     */             try {
/*     */               if (shellInfo != null) {
/*     */                 WarnMailUtil.sendShellInfo(shellInfo);
/*     */               }
/* 247 */             } catch (Exception e) {
/*     */               e.printStackTrace();
/*     */             } 
/*     */           };
/* 251 */         ThreadPoolUtil.executor.execute(runnable);
/*     */       } else {
/*     */         
/* 254 */         this.shellInfoService.updateById(shellInfo);
/*     */       } 
/* 256 */     } catch (Exception e) {
/* 257 */       logger.error("保存下发指令错误", e);
/* 258 */       this.logInfoService.save("保存下发指令错误", e.toString(), "1");
/*     */     } 
/* 260 */     return "redirect:/shellInfo/list";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"edit"})
/*     */   public String edit(Model model, HttpServletRequest request) {
/* 272 */     String errorMsg = "添加下发指令";
/* 273 */     String id = request.getParameter("id");
/* 274 */     ShellInfo ShellInfo = new ShellInfo();
/*     */     try {
/* 276 */       List<SystemInfo> systemInfoList = this.systemInfoService.selectAllByParams(new HashMap<>());
/* 277 */       model.addAttribute("systemInfoList", systemInfoList);
/*     */       
/* 279 */       getBlockStr(model);
/*     */       
/* 281 */       if (StringUtils.isEmpty(id)) {
/* 282 */         model.addAttribute("shellInfo", ShellInfo);
/*     */         
/* 284 */         Map<String, Object> params = new HashMap<>();
/* 285 */         int dbSize = this.shellInfoService.countByParams(params);
/* 286 */         if (dbSize >= 10 && !StaticKeys.LICENSE_STATE.equals("1")) {
/* 287 */           return "redirect:/shellInfo/list?liceFlage=1";
/*     */         }
/*     */         
/* 290 */         return "shellInfo/add";
/*     */       } 
/* 292 */       ShellInfo = this.shellInfoService.selectById(id);
/* 293 */       model.addAttribute("ShellInfo", ShellInfo);
/* 294 */     } catch (Exception e) {
/* 295 */       logger.error(errorMsg, e);
/* 296 */       this.logInfoService.save(errorMsg, e.toString(), "1");
/*     */     } 
/* 298 */     return "shellInfo/add";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"view"})
/*     */   public String view(Model model, HttpServletRequest request) {
/* 311 */     String errorMsg = "查看指令下发执行状态信息错误";
/* 312 */     String id = request.getParameter("id");
/* 313 */     ShellInfo ShellInfo = new ShellInfo();
/*     */     try {
/* 315 */       ShellInfo = this.shellInfoService.selectById(id);
/* 316 */       Map<String, Object> params = new HashMap<>();
/* 317 */       params.put("shellId", id);
/* 318 */       List<ShellState> shellStateList = this.shellStateService.selectAllByParams(params);
/*     */       
/* 320 */       for (ShellState shellState1 : shellStateList) {
/* 321 */         shellState1.setHostname(shellState1.getHostname() + HostUtil.addRemark(shellState1.getHostname()));
/*     */       }
/*     */       
/* 324 */       model.addAttribute("shellStateList", shellStateList);
/* 325 */       model.addAttribute("shellInfo", ShellInfo);
/* 326 */     } catch (Exception e) {
/* 327 */       logger.error(errorMsg, e);
/* 328 */       this.logInfoService.save(errorMsg, e.toString(), "1");
/*     */     } 
/* 330 */     return "shellInfo/stateList";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @ResponseBody
/*     */   @RequestMapping({"cancel"})
/*     */   public String cancel(Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
/* 346 */     String errorMsg = "取消下发指令信息错误";
/* 347 */     ShellInfo ShellInfo = new ShellInfo();
/*     */     try {
/* 349 */       if (!StringUtils.isEmpty(request.getParameter("id"))) {
/* 350 */         ShellInfo = this.shellInfoService.selectById(request.getParameter("id"));
/* 351 */         this.logInfoService.save("取消下发指令信息", "取消下发指令信息：" + ShellInfo.getShell(), "0");
/* 352 */         this.shellInfoService.cancelShell(request.getParameter("id"));
/*     */       } 
/* 354 */     } catch (Exception e) {
/* 355 */       logger.error(errorMsg, e);
/* 356 */       this.logInfoService.save(errorMsg, e.toString(), "1");
/*     */     } 
/* 358 */     return ResDataUtils.resetSuccessJson(null);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @ResponseBody
/*     */   @RequestMapping({"restart"})
/*     */   public String restart(Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
/* 373 */     if (!"true".equals(this.commonConfig.getShellToRun())) {
/* 374 */       return ResDataUtils.resetErrorJson("shellToRun is no open");
/*     */     }
/* 376 */     String errorMsg = "重新下发指令信息错误";
/*     */     try {
/* 378 */       if (!StringUtils.isEmpty(request.getParameter("id"))) {
/* 379 */         ShellInfo shellInfo = this.shellInfoService.selectById(request.getParameter("id"));
/* 380 */         this.logInfoService.save("重新下发指令信息", "重新下发指令信息：" + shellInfo.getShell(), "0");
/* 381 */         this.shellInfoService.restart(request.getParameter("id"));
/*     */         
/* 383 */         Runnable runnable = () -> {
/*     */             try {
/*     */               if (shellInfo != null) {
/*     */                 WarnMailUtil.sendShellInfo(shellInfo);
/*     */               }
/* 388 */             } catch (Exception e) {
/*     */               e.printStackTrace();
/*     */             } 
/*     */           };
/* 392 */         ThreadPoolUtil.executor.execute(runnable);
/*     */       }
/*     */     
/* 395 */     } catch (Exception e) {
/* 396 */       logger.error(errorMsg, e);
/* 397 */       this.logInfoService.save(errorMsg, e.toString(), "1");
/*     */     } 
/* 399 */     return ResDataUtils.resetSuccessJson(null);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"del"})
/*     */   public String delete(Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
/* 413 */     String errorMsg = "删除下发执行信息错误";
/* 414 */     ShellInfo shellInfo = new ShellInfo();
/*     */     try {
/* 416 */       if (!StringUtils.isEmpty(request.getParameter("id"))) {
/* 417 */         String[] ids = request.getParameter("id").split(",");
/* 418 */         for (String id : ids) {
/* 419 */           shellInfo = this.shellInfoService.selectById(id);
/* 420 */           this.logInfoService.save("删除下发指令信息", "删除下发指令信息：" + shellInfo.getShell(), "0");
/*     */         } 
/* 422 */         this.shellInfoService.deleteById(ids);
/*     */       } 
/* 424 */     } catch (Exception e) {
/* 425 */       logger.error(errorMsg, e);
/* 426 */       this.logInfoService.save(errorMsg, e.toString(), "1");
/*     */     } 
/*     */     
/* 429 */     return "redirect:/shellInfo/list";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private void getBlockStr(Model model) {
/* 439 */     String blockStr = "";
/* 440 */     if (!StringUtils.isEmpty(this.commonConfig.getShellToRunLinuxBlock())) {
/* 441 */       blockStr = "[linux不能包含敏感字符:" + this.commonConfig.getShellToRunLinuxBlock() + "]";
/*     */     }
/* 443 */     if (!StringUtils.isEmpty(this.commonConfig.getShellToRunLinuxBlock())) {
/* 444 */       blockStr = blockStr + "[windows不能包含敏感字符:" + this.commonConfig.getShellToRunWinBlock() + "]";
/*     */     }
/* 446 */     model.addAttribute("blockStr", blockStr);
/*     */   }
/*     */ }


/* Location:              /Users/zhangyongxin/soft-install/wgcloud/wgcloud-v3.3.5/server/bank/wgcloud-server-release.jar!/BOOT-INF/classes/com/wgcloud/controller/ShellInfoController.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */